/**
 * 部门管理初始化
 */
var Dept = {
    id: "DeptTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    parentId:0
};
Dept.refresh =function(){
    Dept.table.refresh();
    Dept.ztree.init();
};
/**
 * 初始化表格的列
 */
Dept.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '部门简称', field: 'name', align: 'center', width: '100px', valign: 'middle', sortable: true},
        {title: '部门全称', field: 'fullname', align: 'center', width: '150px', valign: 'middle', sortable: true},
        {title: '部门编码', field: 'code', align: 'center', width: '100px', valign: 'middle', sortable: true},
        {title: 'HIS编码', field: 'hisCode', align: 'center', width: '100px', valign: 'middle', sortable: true},
        {title: '部门领导', field: 'leader', align: 'center', width: '100px', valign: 'middle',formatter:function(value,row,index){
                return row.leaderName;
            }},
        {title: '分管领导', field: 'superior', align: 'center', width: '100px', valign: 'middle',formatter:function(value,row,index){
                return row.superiorName;
            }},
        {title: '排序', field: 'weight', align: 'center', width: '70px', valign: 'middle', sortable: true},
        {title: '备注', field: 'description', align: 'center', valign: 'middle', sortable: true},
        {title: '操作', field: 'id', align: 'center', width: '80px', valign: 'middle',formatter:function(value,row,index){
                var _html = '<a href="javascript:Dept.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
                _html += '<a href="javascript:Dept.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="hisui-icon icon-cancel"></i></a>';
                return _html;
            }}];
};

/**
 * 检查是否选中
 */
Dept.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Dept.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加部门
 */
Dept.add = function () {
    this.layerIndex = Sys.open('新增部门',Sys.backendPath + '/dept/dept_add');
};

/**
 * 点击同步部门
 */
Dept.sync = function () {
    if((Dept.parentId==0)||(Dept.parentId==''))
    {
        Sys.info("请先选中要导入组织机构树的节点！");
        return false;
    }
    //this.layerIndex = Sys.open('同步部门',Sys.backendPath + '/dept/dept_add');
    var operation = function(){
        /*
        var ajax = new $ax(Sys.backendPath + "/dept/sync", function (data) {
            Sys.success("同步成功!记录数量："+ data.message);
            Dept.refresh();
        }, function (data) {
            Sys.error("同步失败!" + data.responseJSON.message + "!");
        });
        ajax.set("deptId", Dept.parentId);
        ajax.start();
        */
        Sys.ajax(Sys.backendPath + "/dept/syncDept",{deptId:Dept.parentId},function(data){
            if(data.code=='200'){
                Sys.success("同步成功!记录数量："+ data.message);
                Dept.refresh();
            }else{
                Sys.error("同步失败！"+data.message);
            }
        }, null,"json",true,"字典数据正在同步中..");
    };
    Sys.confirm("是否同步部门科室病区字典?",operation);
};

/**
 * 打开查看部门详情
 */
Dept.edit = function (id) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        this.layerIndex = Sys.open('修改部门',Sys.backendPath + '/dept/dept_update/' + id);
    }
};

/**
 * 删除部门
 */
Dept.del = function (id,name) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
    if (flag) {
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/dept/delete", function (data) {
                if (data.code==200) {
                    Sys.success("删除成功!");
                    Dept.refresh();
                } else {
                    Sys.error("删除失败!" + data.message + "!");
                }
            }, function (data) {
                Sys.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("deptId", id);
            ajax.start();
        };
        Sys.confirm("是否删除部门【" + name + "】?",operation);
    }
};

/**
 * 查询部门列表
 */
Dept.search = function () {
    var queryData = {};
    queryData['parentId'] = Dept.parentId;
    queryData['condition'] = $("#condition").val();
    Dept.table.refresh({query: queryData});
};

Dept.onClickDept = function (e, treeId, treeNode) {
    Dept.parentId = treeNode.id;
    Dept.search();
};

Dept.expDepts = function () {
    var $df = $("#downForm");
    //提交表单，实现下载
    $df.submit();
};

Dept.impDepts = function () {
    if((Dept.parentId==0)||(Dept.parentId==''))
    {
        Sys.info("请先选中要导入组织机构树的节点！");
        return false;
    }
    var index = layer.open({
        type: 2,
        title: '导入科室',
        area: ['550px', '300px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Sys.backendPath + '/dept/to_import/'+ Dept.parentId
    });
    this.layerIndex = index;
};

Dept.template = function () {
    window.location.href = Sys.backendPath+"/static/backend/system/dept/科室导入模板.xlsx";
};

$(function () {
    var defaultColunms = Dept.initColumn();
    var table = new BSTable(Dept.id, Sys.backendPath + "/dept/list", defaultColunms);
    table.setPaginationType("client");
    table.setSubHeight(-10);
    table.setSort("weight");
    table.init();
    Dept.table = table;
    var ztree = new $ZTree("deptTree", Sys.backendPath + "/dept/tree");
    ztree.bindOnClick(Dept.onClickDept);
    ztree.init();
    Dept.ztree = ztree;
    fuzzySearch('deptTree','#seachKey',null,true);
});
